package com.wonderland.sail.exportexcel;

import com.wonderland.sail.constant.Constant;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

@Controller
public class ExportExcelController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping("exportExcel/exportExcelByHtml")
    public void exportExcelByHtml(ExcelVO excelVO, HttpServletRequest request, HttpServletResponse response) {
        try {
            ExportExcel exportExcel = new ExportExcel(excelVO.getExcelVersion());
            Workbook wb = exportExcel.exportExcelByHtml(excelVO);
            String fileName = String.valueOf(new java.util.Date().getTime()) + "." + excelVO.getExcelVersion();
            OutputStream os = response.getOutputStream();
            response.reset();// 清空输出流
            response.setHeader("Content-disposition", "attachment; filename=" + fileName);// 设定输出文件头
            response.setContentType("application/msexcel");// 定义输出类型
            wb.write(os);
            os.close();
        } catch (Exception e) {
            logger.error(Constant.ERROR, e);
        }
    }
}
